<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class banners extends Modules_Controller
{
   	protected $rules = array(
		"rule" => array(
			array(
				"field" => "title",
				"label" => "标题",
				"rules" => "trim|required"
			),
			array(
				"field" => "photo",
				"label" => "图片",
				"rules" => "trim|required"
			),
            array(
				"field" => "link",
				"label" => "链接",
				"rules" => "trim|callback_back_link"
			),
            array(
                "field" => "t_id",
                "label" => "id值",
                "rules" => "trim|callback_back_number"
            )
		)
	);


    protected function _create_data(){
        $form=$this->input->post();
        $form['timeline'] = time();
        return $form;
    }

    public function back_link($url){
        if($url!=''){
            if(filter_var($url, FILTER_VALIDATE_URL )){
                return true;
            }else{
                $this->form_validation->set_message('back_link', '链接格式错误');
                return false;
            }
        }else{
            return true;
        }
    }

    public function back_number($url){
        if($url!=''){
            if((bool)(preg_match('/^[-\+]?\d+$/',$url))){
                return true;
            }else{
                $this->form_validation->set_message('back_number', 'id值必须为整数');
                return false;
            }
        }else{
            return true;
        }
    }

    public function _edit_data()
    {
        $form=$this->input->post();
        $form['timeline'] = time();
        return $form;
    }

	public function copypro()
    {
        $ids = $this->input->post('ids');

        $rs=$this->model->get_one($ids);

        unset($rs['id']);
        unset($rs['sort_id']);
        unset($rs['timeline']);

        $id = $this->model->create($rs);
        if ($id) {
            $vdata['msg'] = '复制成功，请刷新查看';
            $vdata['status'] = 1;
        }else{
            $vdata['msg'] = '复制失败，请刷新后重试';
            $vdata['status'] = 0;
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }

}
